import time
import datetime
import dateutil.parser

now = time.time()  # 当前时间 or : datetime.datetime.now()

print(now)  # 1532481721.3719301
# 1532481721
# 1587971109337
print(int(now * 1000))
# 1587971070
print(int(now))
'''
time module
'''
localtime = time.localtime(now)
strftime = time.strftime("%Y-%m-%d %H:%M:%S", localtime)
print("strftime --> ", strftime)  # 2018-07-25 09:25:41

'''
datetime module
'''
localtime = datetime.datetime.utcfromtimestamp(now)
strftime = localtime.strftime("%Y-%m-%d %H:%M:%S")
print(strftime)  # 2018-07-25 01:27:53
iosdate = datetime.datetime.utcfromtimestamp(1532481721).isoformat()
print("isodate", iosdate)
now_str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
now_str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S-%s")

print(now_str)  # 2018-07-25 09:30:32

'''
string to date
'''

str_time = "2021-04-25 09:25:41"
strptime = time.strptime(str_time, "%Y-%m-%d %H:%M:%S")

# time.struct_time(
# tm_year=2018, tm_mon=7, tm_mday=25, tm_hour=9, tm_min=25, tm_sec=41, tm_wday=2, tm_yday=206, tm_isdst=-1)
print("strptime -- >", strptime)

mktime = time.mktime(strptime)

print("mktime --> ", mktime)  # 1532481941.0


UTC_time = "2018-08-20 06:20:38+00:00"

time_strftime = time.strptime(UTC_time, "%Y-%m-%d %H:%M:%S+00:00")
print("UTC struct time is : ", time_strftime)
utc_timestamp = time.mktime(time_strftime)
print("utc_timestamp is :", utc_timestamp)
utc_timestamp += 8 * 60 * 60 * 1.0
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(utc_timestamp)))
print(time.gmtime(utc_timestamp))
